<template>
    <el-form ref="form" :rules="rules" :model="form" label-width="100px">
        <el-form-item label="渠道" prop="source">
            <select-app-source v-model="form.source"></select-app-source>
        </el-form-item>
        <el-form-item label="系统" prop="os">
            <select-os v-model="form.os"></select-os>
        </el-form-item>
        <el-form-item label="最小版本号" prop="minVer">
            <el-input v-model="form.minVer" placeholder="最小版本号, 低于此版本则要求强制更新, x.x.x格式"></el-input>
        </el-form-item>
        <el-form-item label="当前版本号" prop="curVer">
            <el-input v-model="form.curVer" placeholder="当前版本号, x.x.x格式"></el-input>
        </el-form-item>
        <el-form-item label="下载包路径" prop="url">
            <el-input v-model="form.url" placeholder="下载包路径"></el-input>
        </el-form-item>
        <el-form-item label="更新说明" prop="comment">
            <el-input type="textarea" v-model="form.comment" placeholder="更新说明"></el-input>
        </el-form-item>
        <el-form-item label="强制更新" prop="force">
            <el-radio-group v-model="form.force">
                <el-radio-button :label="0">否</el-radio-button>
                <el-radio-button :label="1">是</el-radio-button>
            </el-radio-group>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" :loading="loading" @click="submit">确定</el-button>
            <el-button @click="cancel">返回</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    export default {
        props: {
            data: {

            }
        },
        data() {
            return {
                loading: false,
                form: {},
                formDefault: {
                    id: 0,
                },
                rules: {
                    // appid: [
                    //     { required: true, message: '请选择关联应用', trigger: 'blur' },
                    // ],
                    source: [
                        { required: true, message: '请输入关联渠道', trigger: 'blur' },
                    ],
                    os: [
                        { required: true, message: '请选择系统类型', trigger: 'blur' },
                    ],
                    minVer: [
                        { required: true, message: '请输入最小版本号', trigger: 'blur' },
                    ],
                    curVer: [
                        { required: true, message: '请输入当前版本号', trigger: 'blur' },
                    ],
                },
                // options: this.$store.state.app.options,
            }
        },
        computed: {},
        watch: {
            data (newVal) {
                this.syncForm();
            }
        },
        methods: {
            getFormAction() {
                return this.form.id ? 'appVersion/Update' : 'appVersion/Store';
            },
            submit() {
                this.$refs.form.validate(valid => {
                    if (!valid) return false;
                    this.loading = true;
                    this.$store.dispatch(this.getFormAction(), this.form).then(res => {
                        this.$message.success(res.data.message);
                        this.$emit('submit');
                    }).finally(() => {
                        this.loading = false;
                    });
                });
            },
            cancel() {
                this.$emit('cancel');
            },
            syncForm() {
                this.form = this.$helper.deepClone(Object.assign({}, this.formDefault, this.data));
            }
        },
        created () {
            this.syncForm();
        }
    }
</script>

<style lang="scss" scoped>
.item-row {
    margin: 0 0 10px 0;
}
</style>

